package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    DishMapper dishMapper;
    @Autowired
    DishFlavorMapper dishFlavorMapper;
    @Autowired
    SetmealDishMapper setmealDishMapper;

    /**
     * 菜品分页查找
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        //1、开启分页
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        //2、查询
        Page<DishVO> page = dishMapper.pageSelect(dishPageQueryDTO);
        //3、封装结果

        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 新增菜品
     * @param dishDTO
     */
    @Override
    @Transactional
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        //保存菜品表
        dishMapper.save(dish);

        //拿到返回的主键
        Long dishId = dish.getId();
        //保存菜品口味表
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishId));
            //向口味表插入n条数据
            dishFlavorMapper.insert(flavors);
        }
    }

    /***
     * 根据id查询菜品
     * @param id
     * @return
     */
    @Override
    public DishVO getById(Long id) {
        //创建返回前端的对象
        DishVO dishVO = new DishVO();
        //根据id查询菜品表
        Dish dish = dishMapper.getById(id);
        //赋值
        BeanUtils.copyProperties(dish,dishVO);
        //根据菜品id查询口味表，并封装为集合
        List<DishFlavor> flavorList = dishFlavorMapper.getFlavorByDishid(id);
        //将口味集合赋值到dishVO中
        dishVO.setFlavors(flavorList);
        return dishVO;
    }

    /**
     * 修改菜品信息
     * @param dishDTO
     */
    @Override
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        //将DTO中的数据赋值到dish中
        BeanUtils.copyProperties(dishDTO,dish);
        //修改dish表
        dishMapper.update(dish);
        //先删除口味表中的数据
        Long dishId = dishDTO.getId();
        dishFlavorMapper.deleteByDIshID(dishId);
        //再取出DTO中的口味信息，保存至口味表
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() > 0) {
            flavors.forEach(dishFlavor -> dishFlavor.setDishId(dishId));
            //向口味表插入n条数据
            dishFlavorMapper.insert(flavors);
        }
    }

    /**
     * 批量删除菜品
     * @param ids
     */
    @Override
    @Transactional
    public void delete(List<Long> ids) {
        //判断是否存在启售菜品
        for (Long id : ids) {
            Dish dish = dishMapper.getById(id);
            if(dish.getStatus()== StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断是否关联套餐
        for (Long id : ids) {
            int count = setmealDishMapper.getByDishId(id);
            if(count!=0){
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }
        //删除菜品信息
        dishMapper.delete(ids);
        //删除菜品口味信息
        ids.forEach(id->dishFlavorMapper.deleteByDIshID(id));
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<Dish> getByCategoryId(Long categoryId) {
        List<Dish> dishlist = dishMapper.getByCategoryId(categoryId);
        return dishlist;
    }

    /**
     * 菜品启售、停售
     * @param status
     * @param id
     */
    @Override
    public void startorstop(Integer status, Long id) {
        Dish dish = Dish.builder().id(id).status(status).build();
        dishMapper.update(dish);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.getByCategoryId(dish.getCategoryId());

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            //对于管理端停售的商品，用户端不展示的业务处理
            if(d.getStatus()==1) {
                BeanUtils.copyProperties(d, dishVO);

                //根据菜品id查询对应的口味
                List<DishFlavor> flavors = dishFlavorMapper.getFlavorByDishid(d.getId());

                dishVO.setFlavors(flavors);
                dishVOList.add(dishVO);
            }

        }

        return dishVOList;
    }
}
